#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/3/30 14:57
# @Author  : Grayson Liu
# @Email   : graysonliu@foxmail.com
# @File    : CustomMethodListWidget.py

from PyQt5.Qt import *

from ui.Ui_CustomListWidget import Ui_CustomListWidget
from CustomSignal import CustomSignal


class CustomMethodListWidget(QWidget, Ui_CustomListWidget):
    """
    自定义的表格控件，属于QWidget，继承自Ui_CustomListWidget，用于显示方法列表
    """
    global_signal = CustomSignal()

    def __init__(self, parent=None):
        """
        初始化
        :param parent: 父窗体
        """
        super(CustomMethodListWidget, self).__init__(parent)
        self.setupUi(self)

        # 初始化tableWidget
        self.tableWidget.setColumnCount(3)
        self.tableWidget.setHorizontalHeaderLabels(["方法", "参数", "操作"])  # 表头
        self.tableWidget.horizontalHeader().setFont(QFont("宋体", 8, QFont.Black))  # 表头的字体
        self.tableWidget.verticalHeader().setFont(QFont("Arial", 8))  # 列序号的字体
        # 设置表头的宽度
        self.tableWidget.setColumnWidth(0, 100)
        self.tableWidget.setColumnWidth(2, 50)
        self.tableWidget.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch)
        self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)

    def add_list_item(self, name: str, parameters: str):
        """
        对CustomMethodListWidget控件，根据传入的信息，在表格中添加一行
        :param name:
        :param parameters:
        :return:
        """
        item_name = QTableWidgetItem(name)
        item_name.setFont(QFont("宋体", 8))
        item_parameters = QTableWidgetItem(parameters)
        item_parameters.setFont(QFont("Arial", 8))
        row = self.tableWidget.rowCount()
        self.tableWidget.insertRow(row)
        self.tableWidget.setItem(row, 0, item_name)
        self.tableWidget.setItem(row, 1, item_parameters)

        # 新建一个QPushButton对象，放在表格控件的最后一列
        btnDelete = QPushButton("删除")
        btnDelete.setFont(QFont("宋体", 8))
        btnDelete.clicked.connect(self.delete_list_item)
        self.tableWidget.setCellWidget(row, 2, btnDelete)

        self.tableWidget.setRowHeight(row, 25)

    def delete_list_item(self):
        """
        对CustomMethodListWidget控件，根据指定行数，在表格中删除该行
        :return:
        """
        # 弹出对话框，询问用户是否确认删除
        reply = QMessageBox.question(self, "确认删除", "确认删除该方法吗？",
                                     QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
        if reply == QMessageBox.Yes:
            # 获取信号源头，删除CustomMethodListWidget控件中指定的行
            button = self.sender()
            if button:
                index = self.tableWidget.indexAt(button.pos()).row()
                self.tableWidget.removeRow(index)
                # 删除之后，给ProjectForm发信号，进行后续处理
                CustomMethodListWidget.global_signal.delete_method.emit(index)

    def clear_list_item(self):
        """
        清空CustomMethodListWidget控件的所有内容
        :return:
        """
        self.tableWidget.clearContents()
        self.tableWidget.setRowCount(0)
